package ink.lovejinhu.leetcode;

/**
 * @author jinhu
 * created 2021-07-27 14:26
 */
public class Solution_0605 {

    /**
     * 种花
     *
     * @param flowerbed
     * @param n
     * @return
     */
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        for (int i = 0; i < flowerbed.length; ) {
            if(flowerbed[i]==0){
                if (i+1>=flowerbed.length){
                    n--;
                    break;
                }
                if (flowerbed[i+1]!=1){  //i位置可以被种植
                    n--;
                    i+=2;
                }else {
                    i+=3;
                }
            }else if (flowerbed[i]==1){
                i+=2;
                System.out.println(i);
            }else {

            }

        }
        return n<1?true:false;
    }

    public static void main(String[] args) {
        Solution_0605 solution_0605 = new Solution_0605();
//        [1,0,0,0,0,0,1]
//        2
        int[] test={1,0,0,0,0,0,1};
        solution_0605.canPlaceFlowers1(test, 2);

    }
    public boolean canPlaceFlowers1(int[] flowerbed, int n) {
        for (int i = 0; i < flowerbed.length; ) {
            if(flowerbed[i]==0){
                if (i+1<flowerbed.length&&flowerbed[i+1]!=1){  //i位置可以被种植
                    n--;
                    i+=2;
                }else {
                    i+=3;
                }
            }{
                i+=2;
                System.out.println(i);
            }

        }
        return n<1?true:false;
    }
}
